package com.foreveredu.propms.web.struts.action;

import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.District;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.web.struts.actionform.DistrictActionForm;

public class DistrictAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	// 跳转到管理区管理列表页面
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<District> districtList = managementService.findDistricts();
		request.setAttribute("districtList", districtList);

		return mapping.findForward("districtList");
	}

	// 完成到管理区添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DistrictActionForm daf = (DistrictActionForm) form;
		District district = new District();
		PropertyUtils.copyProperties(district, daf);
		
		// 判断当前输入的管理区编号是否合法！
		if (daf.getDistrictId() == null || "".equals(daf.getDistrictId())) {

			request.setAttribute("district", district);
			request.setAttribute("districtIdError", "请输入物业服务处编号！");
			return mapping.findForward("toDistrictList");
		} else if (managementService.findDistrictByDistrictId(daf.getDistrictId()) != null) {
			request.setAttribute("district", district);
			request.setAttribute("districtIdRequest", "该物业服务处编号已存在，请重新输入！");

			return mapping.findForward("toDistrictList");
		}
		if(!Pattern.matches("^[0-9]+$", daf.getDistrictId())){
			request.setAttribute("district", district);
			request.setAttribute("districtIdError", "物业服务处编号必须为数字组成！");
			return mapping.findForward("toDistrictList");
		}

		if (daf.getName() == null || "".equals(daf.getName())) {

			request.setAttribute("district", district);
			request.setAttribute("districtNameError", "请输入管理区名称！");

			return mapping.findForward("toDistrictList");
		}

		managementService.saveDistrict(district);

		return mapping.findForward("toDistrictList");
	}

	// 跳转到管理区修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DistrictActionForm daf = (DistrictActionForm) form;
		District district = managementService.findDistrictById(daf.getId());
		request.setAttribute("district", district);

		return mapping.findForward("districtUpdate");
	}

	// 保存管理区修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DistrictActionForm daf = (DistrictActionForm) form;

		// 判断当前输入的管理区编号是否合法！
		if (!daf.getDistrictId().equals(
				managementService.findDistrictById(daf.getId()).getDistrictId())) { // 判断提交的管理区编号是否被用户修改

			if (daf.getDistrictId() == null || "".equals(daf.getDistrictId())) {

				District district = managementService.findDistrictById(daf
						.getId());
				request.setAttribute("district", district);

				request.setAttribute("districtIdError", "请输入管理区编号！");
				return mapping.findForward("districtUpdate");
			} else if (managementService
					.findDistrictByDistrictId(daf.getDistrictId()) != null) {

				District district = managementService.findDistrictById(daf
						.getId());
				request.setAttribute("district", district);

				request.setAttribute("districtIdRequest", "该编号已存在，请重新输入！");

				return mapping.findForward("districtUpdate");
			}
		}
		
		if(!Pattern.matches("^[0-9]+$", daf.getDistrictId())){
			
			District district = managementService.findDistrictById(daf.getId());
			request.setAttribute("district", district);
			request.setAttribute("districtIdError", "管理区编号必须为数字组成！");
			return mapping.findForward("districtUpdate");
		}
		if (daf.getName() == null || "".equals(daf.getName())) {
			District district = managementService.findDistrictById(daf.getId());
			request.setAttribute("district", district);

			request.setAttribute("districtNameError", "请输入管理区名称！");

			return mapping.findForward("districtUpdate");
		}

		District district = managementService.findDistrictById(daf.getId());
		district.setDistrictId(daf.getDistrictId());
		district.setName(daf.getName());
		managementService.updateDistrict(district);

		return mapping.findForward("toDistrictList");
	}

	// 完成管理区删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DistrictActionForm daf = (DistrictActionForm) form;
		managementService.deleteDistrictById(daf.getId());

		return mapping.findForward("toDistrictList");
	}
	
}
